package com.avic.rong.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.avic.rong.R;
import com.avic.rong.bean.AdvBean;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.example.library.banner.BannerLayout;
import com.example.minato.minastore.utils.AppUtils;

import java.util.List;

/**
 * Created by test on 2017/11/22.
 */


public class WebBannerAdapter extends RecyclerView.Adapter<WebBannerAdapter.MzViewHolder> {

    private Context context;
    private List<AdvBean> dataList;
    private BannerLayout.OnBannerItemClickListener onBannerItemClickListener;

    public WebBannerAdapter(Context context, List<AdvBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }


    public void setOnBannerItemClickListener(BannerLayout.OnBannerItemClickListener onBannerItemClickListener) {
        this.onBannerItemClickListener = onBannerItemClickListener;
    }

    @Override
    public WebBannerAdapter.MzViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MzViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(WebBannerAdapter.MzViewHolder holder, final int position) {
        if (dataList == null || dataList.isEmpty())
            return;

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.cardView.getLayoutParams();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        params.width = width - 100;
        holder.cardView.setLayoutParams(params);

        final int P = position % dataList.size();
        String url = dataList.get(P).getContent();
        ImageView img = (ImageView) holder.imageView;
//        Glide.with(context).load(url).into(img);
        if (!StringUtils.isEmpty(url) && url.contains("http")) {
            Glide.with(context).load(url).placeholder(R.mipmap.common_bg).error(R.mipmap.error).centerCrop().into(img);
        } else {
            Glide.with(context).load(AppUtils.COURSE_IMAGE_URL + url).placeholder(R.mipmap.common_bg).error(R.mipmap.error).centerCrop().into(img);
        }

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBannerItemClickListener != null) {
                    onBannerItemClickListener.onItemClick(P);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        if (dataList != null) {
            return dataList.size();
        }
        return 0;
    }


    class MzViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        CardView cardView;

        MzViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
        }
    }

}
